package com.baosight.sczx.zm.cp.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cp.domain.ZMCPAttachmanag;

public class ServiceZMCP05 extends ServiceEPBase {

	/**
	 * 初始化
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo  = this.query(inInfo);
		// 编制人 -- 登录人
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.planStatus", new String[]{"草拟","生效","作废"},new String[]{"草拟","生效","作废"},true));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.plantype", true));
		return outInfo;
	}
	
	/**
	 * 查询
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		//编制部门		
		StaticTools.setQueryParameters(inInfo, "fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdShow", new BigDecimal(0), false);
		EiInfo outInfo = new EiInfo();
		outInfo=super.query(inInfo, "ZMCPAttachmanag.query", new ZMCPAttachmanag());
		//计划类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.plantype", true));
		//计划月份
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));
		return outInfo;
	}
	
	/**
	 * 插入
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//获取result域的值
		EiInfo out = new EiInfo();
		ZMCPAttachmanag attach = new ZMCPAttachmanag();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(attach.eiMetadata);
		int rows = eiBlock.getRowCount();
		Map<String, String> map = new HashMap<String, String>();
		for (int i=0; i < rows; i++) {
			attach.fromMap(eiBlock.getRow(i));
			map.put("fdShow", attach.getFdShow().toString());
			map.put("fdPlanYear", attach.getFdPlanYear());
			map.put("fdPlanMonth", attach.getFdPlanMonth());
			map.put("fdPlanType", attach.getFdPlanType());
			map.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			//判断是否存在
			boolean validate = this.validate(attach, "ZMCPAttachmanag.query", map, "query");
			if(!validate){
				attach.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				attach.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				attach.setFdCreatorId(UserSession.getUserId().toString());
				attach.setFdCreateDepartment(Department.getInstance().getDepartmentNameById(attach.getFdCreateDepartmentId()));
				attach.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_ATTACHMANAG"));
				attach.setFdDocid(StaticTools.getDocId());
				dao.insert("ZMCPAttachmanag.insert", attach);
				
			}
			else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg(attach.getFdTitle()+"已经存在！");
				return out;
			}
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rows + "记录成功");
		return out;
	}
	/**
	 * 更新
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPAttachmanag attach = new ZMCPAttachmanag();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(attach.eiMetadata);
		int rows = eiBlock.getRowCount();
		Map<String,String> map = new HashMap<String, String>();
		for(int i=0;i<rows;i++){
			attach.fromMap(eiBlock.getRow(i));
			map.put("fdKeyId", attach.getFdKeyId());
			map.put("fdShow", attach.getFdShow().toString());
			map.put("fdPlanYear", attach.getFdPlanYear());
			map.put("fdPlanMonth", attach.getFdPlanMonth());
			map.put("fdPlanType", attach.getFdPlanType());
			map.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			boolean validate = this.validate(attach, "ZMCPAttachmanag.updateValidate", map, "update");
			if(!validate){
				attach.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				attach.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				attach.setFdCreatorId(UserSession.getUserId().toString());
				attach.setFdCreateDepartment(Department.getInstance().getDepartmentNameById(attach.getFdCreateDepartmentId()));
				this.dao.update("ZMCPAttachmanag.update", attach);
				}
			else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg(attach.getFdTitle()+"已经存在！");
				return out;
			}
			}
		out=this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rows + "记录成功");
		return out;
	}
	
	/**
	 * 删除
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		super.delete(inInfo, "ZMCPAttachmanag.delete");
		out.setStatus(1);
		out=this.query(inInfo);
		out.setMsg("删除操作成功");
		return out;
	}
	//上报
	public EiInfo report(EiInfo inInfo){
			//获得计划ID
			String fdProduceplanId = ProducePlan.checkCanReport(inInfo);
			if (fdProduceplanId == null) {
				//生产计划已经生效
				inInfo.getBlock("result").setCell(0, "fdPlanStatus", "作废");
				inInfo.getBlock("result").setCell(0, "fdPulisherId", UserSession.getUserId());
				inInfo.getBlock("result").setCell(0, "fdPulisher", UserSession.getInSessionProperty("UserCname"));
				this.update(inInfo, "ZMCPAttachmanag.updatePlanStatus");
			}
			else{
				//计划页面显示 
				inInfo.getBlock("result").setCell(0, "fdProduceplanId", fdProduceplanId);
				inInfo.getBlock("result").setCell(0, "fdPlanStatus", "草拟");
				inInfo.getBlock("result").setCell(0, "fdPulisherId", UserSession.getUserId());
				inInfo.getBlock("result").setCell(0, "fdPulisher", UserSession.getInSessionProperty("UserCname"));
				inInfo.getBlock("result").setCell(0, "fdShow",new BigDecimal(1));
				this.update(inInfo, "ZMCPAttachmanag.updatePlanStatus");
				//05页面显示
				inInfo.getBlock("result").setCell(0,"fdKeyId",TablePk.getInstance().getNextValue("T_CP_ATTACHMANAG"));
				inInfo.getBlock("result").setCell(0, "fdProduceplanId", null);
				inInfo.getBlock("result").setCell(0, "fdShow",new BigDecimal(0));
				inInfo.getBlock("result").setCell(0, "fdPlanStatus", "上报");
				this.insert(inInfo,"ZMCPAttachmanag.insert");
			}
			EiInfo out = this.query(inInfo);
			if(fdProduceplanId == null) {
				out.setMsg("该生产计划已经发布，上报无效，原纪录作废！");
			}
			else {
				out.setMsg("该项目计划上报成功！");
			}
			return out;
	}
	
	/**
	 * Description: 对主表记录新增，删除，上报前的后台校验
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCPAttachmanag _dom, String sqlName, Map<String, String> params, String type) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if (result != null && result.size() > 0 && !"report".equals(type)) {
			return true;
		}
		return false;
	}

}
